package vip.geeker.iotms.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import vip.geeker.iotms.dto.DeviceTypeDto;
import vip.geeker.iotms.dto.UserDto;
import vip.geeker.iotms.help.BusinessException;
import vip.geeker.iotms.help.Constants;
import vip.geeker.iotms.help.Response;
import vip.geeker.iotms.persistance.model.*;
import vip.geeker.iotms.service.impl.DeviceService;
import vip.geeker.iotms.service.impl.DeviceTypeDataService;
import vip.geeker.iotms.utils.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * redis对象序列化工具
 *
 * @author zhoup
 * @create 2020-04-2711:20:00
 * @Email acacio@foxmail.com
 * To change this template use File | Settings | Editor | File and Code Templates.
 **/
@Controller
@RequestMapping("/device")
public class DeviceController extends BaseController {
    @Resource
    private DeviceService deviceService;
    @Resource
    private DeviceTypeDataService deviceTypeDataService;

    @GetMapping("/type")
    public String toType(DeviceType type) {
        return Constants.DEVICE_TYPE_URL;
    }

    @GetMapping("/typeData")
    public String toTypeData(HttpServletRequest request, Integer id) {
        request.setAttribute("id", id);
        return Constants.DEVICE_TYPE_DATA_URL;
    }

    @GetMapping("/type/queryData")
    @ResponseBody
    public Object queryData(HttpServletRequest request, Integer id) {
        Response response = new Response();
        response.setData(deviceService.selectDeviceTypeDataByTypeId(id));
        response.setCode(0);
        return response;
    }

    @GetMapping("/toAddTypeData")
    public String toaddTypeData() {
        return Constants.ADD_DEVICE_TYPE_DATA_URL;
    }

    @PostMapping("/addDeviceTypeData")
    @ResponseBody
    public Object addDeviceTypeData(DeviceTypeData data) {
        return deviceService.insertDeviceTypeDataSelective(data);
    }

    @GetMapping("/type/data/page")
    @ResponseBody
    public Object queryTypeDataPage(DeviceTypeData data) {
        return deviceService.queryPage(data);
    }

    @GetMapping("/type/toAddDeviceType")
    public String toAddTypeInfo(HttpServletRequest request) {
        request.setAttribute("deviceType", new DeviceType());
        return Constants.ADD_DEVICE_TYPE_URL;
    }

    @PostMapping("/type/addDeviceType")
    @ResponseBody
    public Object addDeviceType(HttpServletRequest request, DeviceType type) {
        type.setCreator(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());
        return deviceService.addDeviceType(type);
    }

    @GetMapping("/type/page")
    @ResponseBody
    public Object queryTypePage(DeviceType type) {
        return new Response<>(deviceService.queryPage(type));
    }

    @GetMapping("/type/tree")
    @ResponseBody
    public Object quertDeviceTypeTree(DeviceTypeDto type) {
        type.setParentId(0);
        List<DeviceTypeDto> list = deviceService.queryDeviceTypeTree(type);
        return new Gson().toJson(list);
    }

    @GetMapping("/type/queryDeviceType")
    public String queryDeviceType(HttpServletRequest request, Integer id) {
        DeviceType deviceType = deviceService.selectDeviceTypeByPrimaryKey(id);
        request.setAttribute("deviceType", deviceType);
        return Constants.ADD_DEVICE_TYPE_URL;
    }

    @RequestMapping("/type/update")
    @ResponseBody
    public Object updateDeviceType(HttpServletRequest request, @RequestBody DeviceType deviceType) {
        try {
            deviceType.setLastUpdTime(new Date());
            deviceType.setModifier(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());
            boolean updateSuc = deviceService.updateByPrimaryKeySelective(deviceType);
            if (updateSuc) {
                return Constants.SUC_JSON;
            }
        } catch (BusinessException e) {
            return Response.error(e.getMessage());
        }
        return Response.error("操作失败");
    }

    @GetMapping("/params")
    public String toParams(DeviceType type) {
        return Constants.DEVICE_PARAMS_URL;
    }

    @GetMapping("/params/page")
    @ResponseBody
    public Object queryParamsPage(DeviceParams params) {
        return new Response<>(deviceService.queryPage(params));
    }

    @GetMapping("/params/toAddDeviceParams")
    public String toAddParams(HttpServletRequest request) {
        request.setAttribute("deviceParams", new DeviceParams());
        return Constants.ADD_DEVICE_PARAMS_URL;
    }

    @PostMapping("/params/addDeviceParams")
    @ResponseBody
    public Object addDeviceParams(HttpServletRequest request, @RequestBody DeviceParams deviceParams) {
        deviceParams.setCreator(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());
        return deviceService.addDeviceParams(deviceParams);
    }

    @GetMapping("/params/queryDeviceParams")
    public String queryDeviceParams(HttpServletRequest request, Integer id) {
        DeviceParams deviceParams = deviceService.selectDeviceParamsByPrimaryKey(id);
        request.setAttribute("deviceParams", deviceParams);
        return Constants.ADD_DEVICE_PARAMS_URL;
    }

    @RequestMapping("/params/update")
    @ResponseBody
    public Object updateDeviceParams(HttpServletRequest request, @RequestBody DeviceParams deviceParams) {
        try {
            deviceParams.setLastUpdTime(new Date());
            deviceParams.setModifier(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());
            boolean updateSuc = deviceService.updateByPrimaryKeySelective(deviceParams);
            if (updateSuc) {
                return Constants.SUC_JSON;
            }
        } catch (BusinessException e) {
            return Response.error(e.getMessage());
        }
        return Response.error("操作失败");
    }

    @GetMapping("/info")
    public String toInfo(DeviceType type) {
        return Constants.DEVICE_INFO_URL;
    }

    @GetMapping("/info/page")
    @ResponseBody
    public Object queryInfoPage(DeviceInfo info) {
        return new Response<>(deviceService.queryPage(info));
    }

    @GetMapping("/info/toAddDeviceInfo")
    public String toAddInfo(HttpServletRequest request) {
        request.setAttribute("deviceInfo", new DeviceInfo());
        return Constants.ADD_DEVICE_INFO_URL;
    }

    @PostMapping("/info/addDeviceInfo")
    @ResponseBody
    public Object addDeviceInfo(HttpServletRequest request, DeviceInfo deviceInfo) {
        return deviceService.addDeviceInfo(deviceInfo);
    }

    @GetMapping("/info/queryDeviceInfo")
    public String queryDeviceInfo(HttpServletRequest request, Integer id) {
        DeviceInfo deviceInfo = deviceService.selectDeviceInfoByPrimaryKey(id);
        request.setAttribute("deviceInfo", deviceInfo);
        return Constants.ADD_DEVICE_INFO_URL;
    }

    @RequestMapping("/info/update")
    @ResponseBody
    public Object updateDeviceInfo(HttpServletRequest request, @RequestBody DeviceInfo deviceInfo) {
        try {
            boolean updateSuc = deviceService.updateByPrimaryKeySelective(deviceInfo);
            if (updateSuc) {
                return Constants.SUC_JSON;
            }
        } catch (BusinessException e) {
            return Response.error(e.getMessage());
        }
        return Response.error("操作失败");
    }

    @GetMapping("/data")
    public String toData() {
        return Constants.DEVICE_DATA_URL;
    }

    @GetMapping("/data/page")
    @ResponseBody
    public Object queryDataPage(DeviceData data) {
        return deviceService.queryDataPage(data);
    }

    @GetMapping("/splitRule")
    public String toSplitRule() {
        return Constants.PARSING_RULE;
    }

    @GetMapping("/splitRule/addSplitRule")
    public String toAddSplitRule(HttpServletRequest request, Integer id) {
        if(ObjectUtils.isNotNullAndEmpty(id)){
            DataSplitRule splitRule = deviceService.queryById(id);
            request.setAttribute("splitRule",splitRule);
        }else {
            request.setAttribute("splitRule",new DataSplitRule());
        }
        return Constants.PARSING_RULE_ADD;
    }

    @GetMapping("/splitRule/page")
    @ResponseBody
    public Response<PageInfo> toSplitRulePageInfo(DataSplitRule dataSplitRule) {
        return deviceService.queryDataSplitRulePage(dataSplitRule);
    }

    @PostMapping("/splitRule/addRule")
    @ResponseBody
    public Object toAddRuleSplitRule(HttpServletRequest request,DataSplitRule dataSplitRule) {
        /*dataSplitRule.setModidier(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());
        dataSplitRule.setCreator(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());*/
        return deviceService.addRuleSplitRule(dataSplitRule);
    }

    @PostMapping("/splitRule/deleteRule")
    @ResponseBody
    public Object toDeleteSplitRule(DataSplitRule dataSplitRule) {
        return deviceService.toDeleteSplitRule(dataSplitRule);
    }

    @GetMapping("/type/getTypeDataListByTypeId")
    @ResponseBody
    public Object toDeleteSplitRule(Integer typeId) {
        return deviceTypeDataService.queryTypeDataList(typeId);
    }

}
